<!--
 * @Description: 
 * @Author: WuShiKang
 * @Date: 2021-04-06 11:43:13
 * @LastEditors: WuShiKang
 * @LastEditTime: 2021-04-13 17:45:27
-->
<template>
  <div>
    <el-button type="primary" @click="toAdd()">新增</el-button>
    <!-- {{categoryData}} -->
    <el-table :data="categoryData" style="width: 100%">
      <el-table-column type="index" width="50" label="序号"> </el-table-column>
      <el-table-column prop="name" label="名称"> </el-table-column>
      <el-table-column prop="description" label="描述"> </el-table-column>
      <el-table-column prop="no" label="排序号"> </el-table-column>
      <el-table-column prop="parentId" label="父栏目"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.row)"
            >编辑</el-button
          >
          <el-button size="small" type="danger" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="title" :visible.sync="visible">
      <el-form :model="form" :rules="rules" ref="form">
        <!-- {{form}} -->
        <el-form-item
          label="栏目名称"
          :label-width="formLabelWidth"
          prop="name"
        >
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="父栏目" :label-width="formLabelWidth">
          <el-select
            v-model="form.parentId"
            placeholder="请选择父栏目 "
            clearable
          >
            <el-option
              v-for="item in categoryData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="序号" :label-width="formLabelWidth">
          <el-input v-model="form.no" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="栏目介绍" :label-width="formLabelWidth">
          <el-input type="textarea" v-model="form.description"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="toSave('form')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapState, mapActions } from "vuex";
export default {
  data() {
    return {
      title: "新针栏目",
      visible: false,
      form: {},
      formLabelWidth: "120px",
      rules: {
        name: [{ required: true, message: "请输入栏目名称", trigger: "blur" }],
      },
    };
  },
  computed: {
    ...mapState("category", ["categoryData"]),
  },
  methods: {
    ...mapActions("category", [
      "findAllCategory",
      "saveOrUpdataCategory",
      "deleteById",
    ]),
    handleEdit(row) {
      // console.log(row);
      this.title = "编辑栏目";
      this.form = { ...row };
      this.visible = true;
    },
     handleDelete(row) {
      // console.log(row);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async() => {
         let { id } = row;
      let res = await this.deleteById({ id });
      if (res.status === 200) {
        this.findAllCategory();
        this.$notify({
          title: "成功",
          message: res.message,
          type: "success",
        });
      }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      
    },
    /**
     * @description: 添加按钮
     * @param {*}
     * @re     turn {*}
     */
    toAdd() {
      this.title = "新增栏目";
      this.form = {};
      this.visible = true;
    },
    toSave(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let res = await this.saveOrUpdataCategory(this.form);
          console.log(res);
          if (res.status === 200) {
            this.findAllCategory();
            this.visible = false;
            this.$notify({
              title: "成功",
              message: res.message,
              type: "success",
            });
          }
        } else {
          alert("error submit!!");
          return false;
        }
      });
    },
  },
  created() {
    this.findAllCategory();
  },
  mounted() {},
};
</script>
<style scoped>
</style>